package cn.sinata.xldutils_kotlin

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.support.v4.app.Fragment
import android.util.Log
import cn.sinata.xldutils_kotlin.utils.permission
import io.reactivex.Flowable
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import org.jetbrains.anko.toast

/**
 * 打印公司日志
 */
fun Any.sysErr(msg:Any?){
    Log.e("sinata","--------"+msg)
}

fun <T> Flowable<T>.ioScheduler(): Flowable<T> = this.subscribeOn(Schedulers.io())
fun <T> Flowable<T>.defaultScheduler(): Flowable<T> = this.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
fun <T> Observable<T>.defaultScheduler(): Observable<T> = this.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())

fun Activity.callPhone(phone:String?){
    val isAllow = permission(Manifest.permission.CALL_PHONE,"拨号权限",15)
    if (isAllow) {
        val p = phone ?: ""
        val intent = Intent(Intent.ACTION_DIAL)
        intent.data = Uri.parse("tel:" + p)
        this.startActivity(intent)
    } else {
        toast("没有拨号权限")
    }

}

fun Fragment.callPhone(phone:String?){
    activity.callPhone(phone)
}